package com.yzh.wanandroid.expand.view

import android.util.Log
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.yzh.wanandroid.component.activity.MainActivity
import com.yzh.wanandroid.component.fragment.*
import com.yzh.wanandroid.utils.*


/**
 *  通过ViewPager2的扩展函数绑定adapter
 */
fun ViewPager2.initViewPager2(activity: MainActivity): ViewPager2 {

    // 禁止滑动
    this.isUserInputEnabled = false

    this.offscreenPageLimit = 4

    adapter = object : FragmentStateAdapter(activity) {
        override fun getItemCount() = 4

        override fun createFragment(position: Int): Fragment {
            Log.i("initViewPager2","position = $position")
            return when (position) {
                PAGER_HOME -> HomePagerFragment.getInstance()
                PAGER_SQUARE -> SquarePagerFragment.getInstance()
                PAGER_PUBLIC -> PublicPagerFragment.getInstance()
                PAGER_PROJECT -> ProjectPagerFragment.getInstance()
                else -> HomePagerFragment.getInstance()
            }
        }
    }
    return this
}

/**
 *  通过底部导航栏来控制viewpager的滚动
 */
fun BottomNavigationView.initNavigationView(action: (Int) -> Unit): BottomNavigationView {
    setOnNavigationItemSelectedListener {
        action.invoke(it.itemId)
        true
    }
    return this
}